home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Pascal / Games / SWar-p / Util.p < prev   
Encoding:
Text File  |  1995-11-22  |  3.3 KB  |  149 lines  |  [TEXT/PJMM]

  1. unit Util;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Events, Menus, Dialogs, Fonts, Resources, Devices,{}
  8.         ToolUtils, SegLoad,Processes,
  9. {$ENDC}
  10.         Globals, SWSound;
  11.  
  12.     function AbsVal (arg: Integer): Integer; {Use "abs" instead}
  13.     function RngRnd (min: Integer; max: Integer): Integer;
  14.     function IsPressed (keyID: Integer): Boolean;
  15.     procedure SetChecks;
  16.     procedure LoadSounds;
  17.     procedure UnloadSounds;
  18.     procedure LoadSound (var soundH: Handle; soundID: Integer; soundName: Str255);
  19.     procedure UnloadSound (soundH: Handle);
  20.     procedure ExitAppl;
  21.     procedure ErasePort (whichPort: CGrafPtr; whatRect: Rect);
  22.     function ColorsEqual (rgbc1, rgbc2: RGBColor): Boolean;
  23.  
  24. implementation
  25.  
  26.     function AbsVal (arg: Integer): Integer;
  27.     begin
  28.         if arg < 0 then
  29.             AbsVal := -arg
  30.         else
  31.             AbsVal := arg;
  32.     end; (* AbsVal() *)
  33.  
  34.     function IsPressed (keyID: Integer): Boolean;
  35.         var
  36.             keys: KeyMap;
  37.     begin
  38.         GetKeys(keys);
  39.         IsPressed := keys[keyID];
  40.     end; (* IsPressed() *)
  41.  
  42.     function RngRnd (min: Integer; max: Integer): Integer;
  43.         var
  44.             rnd: Integer;
  45.             range, t: LongInt;
  46.     begin
  47.         rnd := BitAnd(Random, $7fff);
  48.         range := max - min;
  49.         t := BSR(rnd * range, 15);
  50.         RngRnd := (t + min);
  51.     end; (* RngRnd() *)
  52.  
  53.  
  54.     procedure SetChecks;
  55.     begin
  56.         CheckItem(gFileMenu, 2, gPauseOn);
  57.         CheckItem(gFileMenu, 3, gSoundOn);
  58.     end; (* SetChecks() *)
  59.  
  60.     procedure LoadSound (var soundH: Handle; soundID: Integer; soundName: Str255);
  61.     begin
  62.         soundH := GetResource('snd ', soundID);
  63.         if soundH = nil then
  64.             begin
  65.                 ExitAppl;
  66.             end; (* if *)
  67.         MoveHHi(soundH);
  68.         HLock(soundH);
  69.         HNoPurge(soundH);
  70.     end; (* LoadSound() *)
  71.  
  72.     procedure UnloadSound (soundH: Handle);
  73.     begin
  74.         HUnlock(soundH);
  75.         HPurge(soundH);
  76.         ReleaseResource(soundH);
  77.     end; (* UnloadSound() *)
  78.  
  79.  
  80.     procedure LoadSounds;
  81.  
  82. {     evar Handle            gNewMatchSoundH:xtern;}
  83. {    eHandle            gShotSoundH:xtern;}
  84. {    eHandle            gBoomSoundH:xtern;}
  85. {    eHandle            gEndorsementSndH:xtern;}
  86.  
  87.     begin
  88.         LoadSound(gNewMatchSoundH, 501, '''snd '' New Level (501)');
  89.         LoadSound(gShotSoundH, 502, '''snd'' Shot (502)');
  90.         LoadSound(gBoomSoundH, 503, '''snd'' Boom (503)');
  91.         AInitSnd;
  92.     end; (* LoadSounds() *)
  93.  
  94.     procedure UnloadSounds;
  95. {     evar Handle            gNewMatchSoundH:xtern;}
  96. {    eHandle            gShotSoundH:xtern;}
  97. {    eHandle            gBoomSoundH:xtern;}
  98. {    eHandle            gEndorsementSndH:xtern;}
  99.     begin
  100.         UnloadSound(gNewMatchSoundH);
  101.         UnloadSound(gShotSoundH);
  102.         UnloadSound(gBoomSoundH);
  103.         AStopSnd;
  104.  
  105.     end; (* UnloadSounds() *)
  106.  
  107.  
  108.     procedure ExitAppl;
  109.     begin
  110.         if gPictureWindow <> nil then
  111.             DisposeWindow(WindowPtr(gPictureWindow));
  112.         if (gOSPtr <> nil) then
  113.             begin
  114.                 CloseCPort(gOSPtr);
  115.                 DisposePtr(Ptr(gOSPtr));
  116.             end; (* if *)
  117.         if (gScrapPtr <> nil) then
  118.             begin
  119.                 CloseCPort(gScrapPtr);
  120.                 DisposePtr(Ptr(gScrapPtr));
  121.             end; (* if *)
  122.         ShowCursor;
  123.         FlushEvents(everyEvent, 0);
  124.         UnloadSounds;
  125.         ExitToShell;
  126.     end; (* ExitAppl() *)
  127.  
  128.     procedure ErasePort (whichPort: CGrafPtr; whatRect: Rect);
  129.         var
  130.             savePort: GrafPtr;
  131.     begin
  132.         GetPort(savePort);
  133.         SetPort(GrafPtr(whichPort));
  134.         EraseRect(whatRect);
  135.         SetPort(savePort);
  136.     end; (* ErasePort() *)
  137.  
  138.     function ColorsEqual (rgbc1, rgbc2: RGBColor): Boolean;
  139.     begin
  140.         ColorsEqual := true;
  141.         if (rgbc1.red <> rgbc2.red) then
  142.             ColorsEqual := FALSE
  143.         else if (rgbc1.green <> rgbc2.green) then
  144.             ColorsEqual := FALSE
  145.         else if (rgbc1.blue <> rgbc2.blue) then
  146.             ColorsEqual := FALSE;
  147.     end; (* ColorsEqual() *)
  148.  
  149. end.